package com.pactera.jep.service.sys.web.controller;

import com.pactera.jep.autoconfigure.web.controller.BaseRestController;
import com.pactera.jep.core.exception.ServiceException;
import com.pactera.jep.orm.Filter;
import com.pactera.jep.orm.Order;
import com.pactera.jep.orm.Page;
import com.pactera.jep.sys.model.NotifyTodoOper;
import com.pactera.jep.sys.service.NotifyTodoOperService;
import com.pactera.jep.web.code.WebCode;
import com.pactera.jep.web.code.WebHeaderCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Yangyao
 * @time 2018年06月28日 11:09
 * @company 文思海辉技术有限公司
 */
@Api(value = "待办配置")
@RestController
@RequestMapping("/notifyTodoOper")
public class NotifyTodoOperRestController extends BaseRestController<NotifyTodoOper> {
    Logger logger = LoggerFactory.getLogger(NotifyTodoOperRestController.class);

    @Autowired
    private NotifyTodoOperService notifyTodoOperService;

    /**
     * 为列表页面做数据准备
     */
    @Override
    protected void prepareQuery(@ApiIgnore @RequestHeader(WebHeaderCode.X_USER_TOKEN) String token, Page<NotifyTodoOper> page, List<Filter> filters, List<Order> orders, HttpServletRequest request) {

        notifyTodoOperService.query(page, filters, orders);
    }

    /**
     * 保存
     * @param notifyTodoOper
     * @return
     */
    @ApiOperation(value = "保存")
    @PostMapping(value = "/save")
    public Map<String, Object> save(@RequestBody NotifyTodoOper notifyTodoOper) {

        Map<String, Object> tips = new HashMap<String, Object>();
        Boolean isExist = true;
        if(notifyTodoOper.getId() == null){
            isExist = false;
        }else{
            isExist = notifyTodoOperService.exists(notifyTodoOper.getId());
        }

        try
        {
            if(isExist)
            {
                notifyTodoOperService.updateSelective(notifyTodoOper);
            }
            else
            {
                notifyTodoOperService.insert(notifyTodoOper);
            }

            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
        }
        catch (ServiceException e)
        {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
        }
        return tips;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除")
    @PostMapping(value = "/remove")
    public Map<String, Object> remove(String id){

        Map<String, Object> tips = new HashMap<String, Object>();

        try
        {
            notifyTodoOperService.deleteByPrimaryKey(id);
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_DELETE);
        }
        catch (ServiceException e)
        {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_DELETE);
        }

        return tips;
    }

    /**
     * 验证提交的id是否已经存在数据库中
     * @param id
     * @return
     */
    @ApiOperation(value = "验证提交的id是否已经存在数据库中")
    @GetMapping(value = "/check", produces = "application/json;charset=UTF-8")
    public Map<String, Object> check(String id){

        Map<String, Object> tips = new HashMap<String, Object>();

        NotifyTodoOper exist = notifyTodoOperService.get(id);
        if(exist == null)
        {
            tips.put("valid", true);
        }
        else
        {
            tips.put("valid", false);
        }

        return tips;
    }
}
